home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / stdio / fmemopen.c < prev    next >
C/C++ Source or Header  |  1993-05-27  |  3KB  |  109 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26.  
  27. /* Defined in fopen.c.  */
  28. extern int EXFUN(__getmode, (CONST char *mode, __io_mode *mptr));
  29.  
  30. /* Open a new stream that will read and/or write from the buffer in
  31.    S, which is of LEN bytes.  If the mode indicates appending, the
  32.    buffer pointer is set to point to the first '\0' in the buffer.
  33.    If S is NULL, the buffer is allocated by malloc and will be freed
  34.    when the stream is closed.  The only purpose of this is to write
  35.    things and then read what's been written.  If LEN is zero, writes will
  36.    always return errors and reads will always return end-of-file.
  37.  
  38.    The stream is set up such that seeks and tells will always fail and
  39.    once the buffer is full of written characters or empty of characters
  40.    to read, attempted writes always return an output error and attempted
  41.    reads always return end-of-file.  */
  42. FILE *
  43. DEFUN(fmemopen, (s, len, mode),
  44.       PTR s AND size_t len AND CONST char *mode)
  45. {
  46.   __io_mode m;
  47.   register FILE *stream;
  48.  
  49.   if (!__getmode (mode, &m))
  50.     return NULL;
  51.  
  52.   stream = __newstream ();
  53.   if (stream == NULL)
  54.     return NULL;
  55.  
  56.   stream->__mode = m;
  57.  
  58.   /* Input gets EOF.  */
  59.   stream->__room_funcs.__input = NULL;
  60.   /* Output gets error.  */
  61.   stream->__room_funcs.__output = NULL;
  62.  
  63.   /* Do nothing for close.  */
  64.   stream->__io_funcs.__close = NULL;
  65.   /* Can't seek outside the buffer.  */
  66.   stream->__io_funcs.__seek = NULL;
  67.   /* There is no associated file descriptor to fetch.  */
  68.   stream->__io_funcs.__fileno = NULL;
  69.  
  70.   stream->__seen = 1;
  71.  
  72.   stream->__userbuf = s != NULL && len > 0;
  73.   if (s == NULL)
  74.     {
  75.       s = malloc (len);
  76.       if (s == NULL)
  77.     {
  78.       int save = errno;
  79.       (void) fclose (stream);
  80.       errno = save;
  81.       return NULL;
  82.     }
  83.     }
  84.  
  85.   stream->__buffer = (char *) s;
  86.   stream->__bufsize = len;
  87.  
  88.   stream->__bufp = stream->__buffer;
  89.   stream->__get_limit = (stream->__buffer +
  90.              (stream->__mode.__read ? stream->__bufsize : 0));
  91.   stream->__put_limit = (stream->__buffer +
  92.              (stream->__mode.__write ? stream->__bufsize : 0));
  93.   stream->__cookie = NULL;
  94.  
  95.   if (stream->__mode.__append)
  96.     {
  97.       char *p = memchr (stream->__bufp, '\0',
  98.             stream->__get_limit - stream->__bufp);
  99.       if (p == NULL)
  100.     stream->__bufp = stream->__get_limit;
  101.       else
  102.     stream->__bufp = p;
  103.     }
  104.   else if (stream->__mode.__truncate)
  105.     memset ((PTR) stream->__buffer, 0, len);
  106.  
  107.   return stream;
  108. }
  109.